<?php

/**
 * api control
 * @author wangXi <iwisunny@gmail.com>
 * Date: 2015/12/22 10:42
 */
class Api extends Base_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function _log($msg){
        file_put_contents('/tmp/kiosk_status.log', $msg, FILE_APPEND);
    }

    function index(){
        $this->beacon();
    }

    function beacon(){
        $this->load->model('kiosk_status_model', 'kiosk_status');
        $this->load->model('kiosk_log_model', 'kiosk_log');

        $client_ip=get_client_ip();
        $kiosk_id=intval($this->input->post_get('kioskId'));
        $packet=$this->input->post_get('data');

        if(empty($packet)){
            $this->put_msg(array(
               'info'=> '[PONG]no packet data'
            ));
        }
        $packet=json_decode($packet, true);

        $cur_kiosk_status=array(
            'cpu_usage'=> $packet['cpu_used'],
            'memory_usage'=> $packet['mem_used'],
            'kiosk_ip'=> get_client_ip(),
            'disk_free_size'=> $packet['disk_free_size'],
            'udm_rental_started'=> $packet['udm_rental_started'],
            'udm_controller_started'=> $packet['udm_controller_started'],
            'last_sync_time'=> $packet['last_sync_time'],
            'net_recv_bytes'=> $packet['net_recv_bytes'],
            'net_send_bytes'=> $packet['net_send_bytes'],
            'last_boot_time'=> $packet['last_boot_time'],
            'ubx_version'=> $packet['ubx_version']
        );

//        $this->_log(json_encode($cur_kiosk_status));

        $this->kiosk_status->flush_row($kiosk_id, $cur_kiosk_status);

        //todo log做定时清理，避免表数据增长过大
        $this->kiosk_log->insert(array(
            'kiosk_id'=> $kiosk_id,
            'log_msg'=> json_encode($packet),
            'log_time'=>time()
        ), true);

        $this->put_msg(array(
            'info'=>'[PONG]kiosk status recv',
            'success'=> true,
            'result'=> $packet
        ));

    }
}